<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      ①：确定终止条件 sum==n&&path.length==k
      ②：确定遍历范围 每个数字只能用一次，因此需要有start
      */
      var combinationSum3 = function (k, n) {
        let res = []
        function backTracking(path, start, sum) {
          if (sum == n && path.length == k) {
            return res.push([...path])
          }
          for (let i = start; i <= 9; i++) {
            path.push(i)
            backTracking(path, i + 1, sum + i)
            path.pop()
          }
        }
        backTracking([], 1, 0)
        return res
      }
    </script>
  </body>
</html>
